package io.github.viscent.mtpattern.ch10.tss.example;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * 基于Thread Specific Storage模式的强随机数生成器代码
 */
public class ThreadSpecificSecureRandom {

    /**
     * 该类的唯一实例
     */
    public static final ThreadSpecificSecureRandom INSTANCE = new ThreadSpecificSecureRandom();

    private ThreadSpecificSecureRandom() {
    }

    /**
     * SECURE_RANDOM相当于模式角色：ThreadSpecificStorage.TSObjectProxy。
     * SecureRandom相当于模式角色：ThreadSpecificStorage.TSObject。
     */
    private static final ThreadLocal<SecureRandom> SECURE_RANDOM = ThreadLocal.withInitial(() -> {
        SecureRandom srnd;
        try {
            srnd = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            srnd = new SecureRandom();
        }
        return srnd;
    });

    public int nextInt(int upperBound) {
        SecureRandom secureRnd = SECURE_RANDOM.get();
        return secureRnd.nextInt(upperBound);
    }

    public void setSeed(long seed) {
        SecureRandom secureRnd = SECURE_RANDOM.get();
        secureRnd.setSeed(seed);
    }

}